#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

//This Unit Test Will Test the Refactored Village Card.

//initialize game struct to array of 0's
void clearGameState(struct gameState *G) {
	int i;
	for (i=0; i<sizeof(struct gameState); i++) {
		((char *)G)[i] = 0;
	}
}

int main() {
	int i, j;
	int currentPlayer, handCount, deckCount;	//relevant to refactored village card in dominion.c
	int seed = 1003;		//FEEL FREE TO CHANGE
	//int unmatch = 0; 		//holds whether or not game states match when being compared
	int num_tests = 100;	//FEEL FREE TO CHANGE
	int errors = 0;			//number of error'd tests
	
	//This test will run by initializing two game states, and comparing them by calling the function on one.
	struct gameState G, G2;
	
	//initialize random number using rngs.c library
	SelectStream(1);
	PutSeed((long)seed);
	
	printf("Now testing cardtest3 (Village card)\n");
	
	for (i=0; i<num_tests; i++) {
	  
	 //initialize two blank game states, (that match)
	 clearGameState(&G);
	 clearGameState(&G2);
	 //unmatch = 0;
	 
	 //get random player number 2-4, and assign them to the appropriate turn in the game states.
	 currentPlayer = (int)(Random()*2)+2;
	 G.whoseTurn = G2.whoseTurn = currentPlayer;
	 
	 //randomize the number of actions, and assign them in the game states.
	 G.numActions = G2.numActions = (int)(Random()*2);
	 
	 //get random hand size and assign in both game states
	 handCount = (int)(Random()*MAX_HAND);
	 G.handCount[currentPlayer] = G2.handCount[currentPlayer] = handCount;
	 
	 //get random cards to fill these handcounts and assign in the game states
	 for (j=0; j<handCount; j++) {
	  G.hand[currentPlayer][j] = G2.hand[currentPlayer][j] = (int)(Random()*MAX_HAND); 
	 }
	 
	 //get random deck size and assign in both game states (and make sure it is at least >= 3)
	 deckCount = (int)((Random()*MAX_HAND)+3);
	 G.deckCount[currentPlayer] = G2.deckCount[currentPlayer] = deckCount;
	 
	 //get random cards to fill these deckcounts and assign in the game states
	 for (j=0; j<deckCount; j++) {
	  G.deck[currentPlayer][j] = G2.deck[currentPlayer][j] = (int)(Random()*MAX_HAND); 
	 }
	 
	 //put village card in each player's hand.
	 G.handCount[currentPlayer]++;
	 G2.handCount[currentPlayer]++;
	 G.hand[currentPlayer][handCount] = G2.hand[currentPlayer][handCount] = village;
	 
	 //Call village function on ONLY GameState 1.
	cardEffect(village, 0, 0, 0, &G, handCount, NULL);
	 
	 //ALTERNATIVELY CALL THIS FUNCTION: new_village(currentPlayer, handCount, &G);
	 
	 //Now that G has had the Village card played, test ALL things done by the Village Card by comparing G and G2
	 //Test Actions was incremented by two
	 if (G.numActions != G2.numActions + 2) {
	   printf("Error, expected to have %d actions, instead we have %d cards\n", G2.numActions + 2, G.numActions);
	   errors++;
	 }
	 
	 //Test that a card was successfully added. Since a card was added, and then a discard occurred, the handCounts should be equal (instead of off by one)
	 if (G.handCount[currentPlayer] != (G2.handCount[currentPlayer])) {
	   printf("Error, expected hand to have %d cards, instead it has %d cards\n", G2.handCount[currentPlayer], G.handCount[currentPlayer]);
	   errors++;
	 }

	} //end main loop
	
	printf("Out of %d tests, %d errors were returned\n\n", num_tests, errors);
  
  return 0;
}

/*For reference

int new_village(int currentPlayer, int handPos, struct gameState *state) {

      //+1 Card
      drawCard(currentPlayer, state);
			
      //+2 Actions
      state->numActions = state->numActions + 2;
			
      //discard played card from hand
      discardCard(handPos, currentPlayer, state, 0);
	  
	  return 0;
}*/